package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.dto.InsertCourseDto;
import cn.itcast.user.dto.UpdateDto;
import cn.itcast.user.pojo.User;
import cn.itcast.user.pojo.course;
import cn.itcast.user.service.UserService;
import cn.itcast.user.service.cService;
import cn.itcast.user.login.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Slf4j
@RestController
//@RefreshScope
public class UserController {


    @Autowired
    private UserService userService;
    @Autowired
    private cService cSever;

    @Autowired
    private cn.itcast.user.mapper.cMapper cMapper;

//    @Value("${pattern.dateformat}")
//    private String dateformat;
    @Autowired
    private PatternProperties patternProperties;

    @GetMapping ("/insertCourse")
    public String insertCourse(@RequestBody InsertCourseDto insertCourseDto){
        course c=new course();
        c.setCourse(insertCourseDto.getCourse());
        c.setAddress(insertCourseDto.getAddress());
        c.setCid(insertCourseDto.getCid());
        c.setMno(insertCourseDto.getMno());
        c.setCt(insertCourseDto.getCt());
        int i=cMapper.insertCourse(c);
        return "ok";

    }

    @GetMapping("/updateCourse")
    public String updateCourse(@RequestBody UpdateDto updateDto){
        course c=new course();
        c.setCourse(updateDto.getCourse());
        c.setAddress(updateDto.getAddress());
        c.setCid(updateDto.getCid());
        c.setCt(updateDto.getCt());
        int i=cMapper.updateCourse(c);
        return "ok";
    }


    @GetMapping("/prop")
    public PatternProperties patternProperties(){
        return patternProperties;
    }


//    @PostMapping("/getuser")
//    public re sssss(@RequestBody @Validated User user){
//        System.out.println(1111);
//        return userService.getuser(user);
//    }

//    @PostMapping("/getuser")
//    public ne ss(User user){
//        return userService.queryById(user);
//    }

    @GetMapping("/now")

    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
    }

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/user/{id}")
    public User queryById(@PathVariable("id") int id) {
        return userService.queryById(id);
    }

/*    @GetMapping("/login/{id}")
    public login queryById1(@PathVariable("id") int kid) {
        return userService.queryById1(kid);
    }*/

    @PostMapping("/login")
    public R login(
            @RequestBody Map<String,Object> map,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        String kid = (String) map.get("kid");
        String password = (String) map.get("password");
//        System.out.println(kid+password);
        User user=userService.queryById1(111111);
        if (user.getPassword().equals(password)){
            return R.se(cSever.getListCourse(1));
        }else {
            return R.df();
        }
    }

    @PostMapping("/course")
    public R course(
            @RequestBody Map<String,Object> map,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        return R.se(cSever.getListCourse(1));
    }

}
